package eu.tanov.epf.pv.types.projectiteration.ui.pages;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.eclipse.epf.authoring.ui.editors.MethodElementEditor;
import org.eclipse.epf.library.edit.command.IActionManager;
import org.eclipse.epf.uma.CustomCategory;
import org.eclipse.epf.uma.Section;
import org.eclipse.epf.uma.UmaFactory;
import org.eclipse.epf.uma.UmaPackage;
import org.eclipse.ui.forms.editor.FormEditor;

import eu.tanov.epf.pv.service.types.handler.CustomTypeHandler;
import eu.tanov.epf.pv.service.types.service.CustomTypeHandlersService;
import eu.tanov.epf.pv.types.projectiteration.common.util.ProjectIterationHelper;
import eu.tanov.epf.pv.types.projectiteration.ui.ProjectIterationActivator;
import eu.tanov.epf.pv.types.projectiteration.ui.i18n.ProjectIterationUIResources;
import eu.tanov.epf.pv.types.projectiteration.ui.provider.ProjectIterationItemProvider;
import eu.tanov.epf.pv.types.projectpractice.common.util.ProjectPracticeHelper;
import eu.tanov.epf.pv.ui.common.pages.AbstractCustomTypeCategoryPage;
import eu.tanov.epf.pv.ui.common.util.EditorHelper;

public class ProjectIterationProjectPracticesPage extends
		AbstractCustomTypeCategoryPage<CustomCategory> {
	private static final String FORM_PAGE_ID = "projectIterationProjectPracticesPage"; //$NON-NLS-1$	

	protected IActionManager actionMgr;

	public ProjectIterationProjectPracticesPage(FormEditor editor) {
		super(
				editor,
				FORM_PAGE_ID,
				ProjectIterationUIResources.projectIterationProjectPracticesPage_title,
				getTypeHelper(),
				ProjectIterationUIResources.projectIteration_text);
		EditorHelper.updateTitleImage(editor,
				ProjectIterationItemProvider.getProjectIterationImage());

		if (editor instanceof MethodElementEditor) {
			actionMgr = ((MethodElementEditor) editor).getActionManager();
		}
	}

	private static CustomTypeHandler<CustomCategory> getTypeHelper() {
		final CustomTypeHandlersService service = ProjectIterationActivator
				.getDefault().getService(CustomTypeHandlersService.class);
		return service.getHandlerForType(ProjectPracticeHelper.getCustomType(),
				CustomCategory.class);
	}

	@Override
	protected String tabString() {
		// FIXME: filter techniques
		return ProjectIterationUIResources.projectIterationProjectPracticesPage_filterTabString;
	}

	@Override
	protected String multipleSelectDescription(int count) {
		return ProjectIterationUIResources
				.bind(ProjectIterationUIResources.projectIterationProjectPracticesPage_multipleSelectDescription,
						new Integer(count));
	}

	@Override
	protected String sectionDescription() {
		return ProjectIterationUIResources.projectIterationProjectPracticesPage_sectionDescription;
	}

	@Override
	protected String sectionName() {
		return ProjectIterationUIResources.projectIterationProjectPracticesPage_sectionName;
	}

	@Override
	protected String selectedLabel() {
		return ProjectIterationUIResources.projectIterationProjectPracticesPage_selectedLabel;
	}

	@Override
	protected String[] modelStructurePath() {
		return ProjectIterationHelper.PROJECT_ITERATIONS_PATH;
	}

	@Override
	protected void addItemsToModel1(
			@SuppressWarnings("rawtypes") ArrayList objectsToAdd) {
		super.addItemsToModel1(objectsToAdd);
		addPracticeSections(objectsToAdd);
	}

	protected void addPracticeSections(List objectsToAdd) {
		for (Object o : objectsToAdd) {
			if (ProjectPracticeHelper.isProjectPractice(o)) {
				CustomCategory cat = (CustomCategory) o;
				// XXX - practice persistence
				List<Section> sections = contentElement.getPresentation()
						.getSections();
				Section newMethod = UmaFactory.eINSTANCE.createSection();
				newMethod.setSectionName(cat.getName());
				setSection(sections, newMethod);
			}
		}

		// HAX
		actionMgr
				.doAction(IActionManager.SET, methodUnit,
						UmaPackage.eINSTANCE.getMethodUnit_ChangeDate(),
						new Date(), -1);
	}

	private void setSection(List<Section> sections, Section newSection) {
		for (int i = 0; i < sections.size(); ++i) {
			if (sections.get(i).getSectionName()
					.equals(newSection.getSectionName())) {
				return;
			}
		}

		sections.add(newSection);
	}
}
